package com.magicsoft.share.ui.my;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.magicsoft.share.R;
import com.magicsoft.share.base.BaseActivity;
import com.magicsoft.share.bean.my.UserInfo;
import com.magicsoft.share.constant.AppDavikActivityMgr;
import com.magicsoft.share.constant.Constant;
import com.magicsoft.share.customview.alertview.AlertView;
import com.magicsoft.share.customview.alertview.OnItemClickListener;
import com.magicsoft.share.customview.dialog.DialogShare;
import com.magicsoft.share.presenter.my.SettingPresenter;
import com.magicsoft.share.ui.main.LoginActivity;
import com.magicsoft.share.ui.main.MainActivity;
import com.magicsoft.share.utils.DataCleanManager;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.utils.ToastUtils;
import com.magicsoft.share.utils.WxShareUtil;
import com.magicsoft.share.view.my.ISettingView;
import com.makeramen.roundedimageview.RoundedImageView;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity<ISettingView, SettingPresenter> implements ISettingView {


    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.ibtn_left)
    ImageButton ibtnLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ibtn_right)
    ImageButton ibtnRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_head)
    RoundedImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sign)
    TextView tvSign;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.layout_user)
    RelativeLayout layoutUser;
    @BindView(R.id.layout_password)
    RelativeLayout layoutPassword;
    @BindView(R.id.layout_cache)
    RelativeLayout layoutCache;
    @BindView(R.id.layout_share)
    RelativeLayout layoutShare;
    @BindView(R.id.layout_good)
    RelativeLayout layoutGood;
    @BindView(R.id.layout_opinion)
    RelativeLayout layoutOpinion;
    @BindView(R.id.btn)
    Button btn;
    private int mResultCode;
    private AlertView alertView;
    private String URL;
    @Override
    public SettingPresenter initPresenter() {
        return new SettingPresenter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {
        tvTitle.setText("设置");
        mPresenter.loginshare();
        try {
            tvCache.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        initDialog();

    }

    @Override
    public void requestData() {

        mPresenter.userDetail();
    }


    @OnClick({R.id.ibtn_left, R.id.layout_user, R.id.layout_password, R.id.layout_cache, R.id.layout_share, R.id.layout_good, R.id.layout_opinion, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ibtn_left:
                finish();
                break;
            case R.id.layout_user:
                startActivityForResult(new Intent(this, PersonalInfoActivity.class), Constant.REFRESH_UI);
                break;
            case R.id.layout_password:
                startActivity(new Intent(this, OldPasswordActivity.class));
                break;
            case R.id.layout_cache:
                DataCleanManager.clearAllCache(getApplicationContext());
                try {

                    tvCache.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
                    ToastUtils.showToast(this, "清理完成");

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.layout_share:
                startActivity(new Intent(this,LoginShareActivity.class));
//                DialogShare mDialogShare = DialogShare.newInstance();
//                mDialogShare.setOnButtonClickListener(new DialogShare.OnButtonClickListener() {
//                    @Override
//                    public void shareToWechatFriend() {
//                        Resources res = getResources();
//                        Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.logo_my);
//                        WxShareUtil.getInstance().init(SettingActivity.this).sendToWxFriend(URL, "分享注册送豪礼",bmp);
//                    }
//
//                    @Override
//                    public void shareToFriendCircle() {
//                        Resources res = getResources();
//                        Bitmap    bmp = BitmapFactory.decodeResource(res, R.drawable.logo_my);
//                        WxShareUtil.getInstance().init(SettingActivity.this).sendToFriendCircle(URL, "分享注册送豪礼", bmp);
//                    }
//                });
//                mDialogShare.show(getSupportFragmentManager(), "");
                break;
            case R.id.layout_good:
                break;
            case R.id.layout_opinion:
                startActivity(new Intent(this, OpinionActivity.class));
                break;
            case R.id.btn:
                alertView.show();
                break;
            case R.id.layout_xin:
                ToastUtils.showToast(this,"给个好评");
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        setResult(mResultCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == RESULT_OK) {

            if (requestCode == Constant.REFRESH_UI) {
                requestData();
                mResultCode = RESULT_OK;
            }
        }

    }

    @Override
    public void userDetailSuccess(UserInfo userInfo) {
        tvName.setText(userInfo.getNickname());
        Glide.with(this).load(userInfo.getHeadimg()).into(ivHead);
        tvSign.setText(userInfo.getPersonalized_signature());
    }

    @Override
    public void loginShare(String url) {
        URL = url;
        if (URL != null) {

        }
    }

    private void initDialog() {
        alertView = new AlertView.Builder().setContext(this).setTitle("是否退出？").setStyle(AlertView.Style.Alert).setCancelText("否").setOthers(new String[]{"是"}).setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(Object o, int position) {

                if (position == -1) {
                    alertView.dismiss();

                } else {
                    alertView.dismiss();
                    AppDavikActivityMgr.getScreenManager().finishActivity(MainActivity.class);
                    SPUtils.getInstance().clear();
                    startActivity(new Intent(SettingActivity.this, LoginActivity.class));
                    finish();

                }
            }
        }).build();
    }


}
